package per.srp.demo.web.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import per.srp.demo.carLink.CarCommandHandler;
import per.srp.demo.carLink.entity.Car;
import per.srp.demo.carLink.entity.CarCommand;
import per.srp.demo.web.controller.entity.ReturnPack;
import per.srp.demo.web.controller.utils.CommandStringTool;

@CrossOrigin(origins = "*")
@RestController
public class CarCommandController {
    @RequestMapping("/sendCommand")
    public ReturnPack sendCommand(String carNo, String commandString){
        carNo = carNo.replaceAll("[\\s\\n\\r]+","");

        commandString = commandString.toLowerCase();

        String wrongPart = CommandStringTool.getWrongPart(commandString);

        System.out.println(carNo+" "+commandString);
        if(wrongPart!=null){
            System.out.println(carNo+" wrong argument "+wrongPart);
            return new ReturnPack(false,"wrong argument",wrongPart);
        }else if(carNo==null||"".equals(carNo)){
            System.out.println(carNo+" wrong carno "+wrongPart);
            return new ReturnPack(false,"wrong carno","");
        }else{
            CarCommand[] commands = CommandStringTool.StringToCommands(commandString);
            CarCommandHandler.pushCarCommand(new Car(carNo,0.0,0.0,0.0,0),commands);//提交命令
        }
        return new ReturnPack(true,"",null);
    }
}
